package xyz.zcq.zcqrpc.registry;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KV;
import io.etcd.jetcd.KeyValue;
import io.etcd.jetcd.kv.DeleteResponse;
import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.kv.PutResponse;

import java.util.List;

public class EtcdRegistryTest {

    public static void main(String[] args) {
        try (Client client = Client.builder().endpoints("http://localhost:2379").build(); KV kvClient = client.getKVClient()) {
            String key = "test_key";
            String value = "test_value";
            ByteSequence keyBs = ByteSequence.from(key.getBytes());
            ByteSequence valueBs = ByteSequence.from(value.getBytes());
            PutResponse putResponse = kvClient.put(keyBs, valueBs).get();

            GetResponse getResponse = kvClient.get(keyBs).get();
            List<KeyValue> kvs = getResponse.getKvs();
            for (KeyValue kv : kvs) {
                String k = kv.getKey().toString();
                String v = kv.getValue().toString();
                System.out.println("k" + ": " + v);
            }

            DeleteResponse deleteResponse = kvClient.delete(keyBs).get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
